package com.qing.common.utils.qcode;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.awt.*;
import java.io.File;

/**
 * @author guoqf
 */
public class HutoolQrCode {


    public static void main(String[] args) {
        String filePath = "E:\\qrcode\\baidu.png";
        String content = "http://www.baidu.com";

        HutoolQrCode qrCode = new HutoolQrCode();
        qrCode.writeQrCodeToFile(qrCode.getQrCodeInfo(), filePath);
    }


    private QrConfig getQrConfig() {

        QrConfig qrConfig = QrConfig.create();
        qrConfig.setWidth(600).setHeight(600).setForeColor(Color.BLACK).setBackColor(Color.WHITE);
        return qrConfig;
    }

    private void writeQrCodeToFile(String content, String filePath) {
        QrCodeUtil.generate(content, getQrConfig(), new File(filePath));
    }

    private String getQrCodeInfo() {
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setName("Qingfeng").setAddress("Hebei").setUrl("http://www.baidu.com");
        return JSONUtil.toJsonStr(qrCodeInfo);
    }


    @Data
    @Accessors(chain = true)
    static class QrCodeInfo {

        private String name;

        private String address;

        private String url;

    }
}
